package com.capgemini.teamtwo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.capgemini.teamtwo.entity.SysUser;
import com.capgemini.teamtwo.entity.User;
import com.capgemini.teamtwo.mapper.SysUserMapper;
import com.capgemini.teamtwo.mapper.UserMapper;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Pie;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api("返回饼图")
@RestController
@RequestMapping("/echart")
public class EchartsController {
    @Resource
    SysUserMapper sysUserMapper;
    /**
         * 饼图 测试用例
         * @return
         */
        @RequestMapping(value = "/pie/{projectid}",method = RequestMethod.POST,
        produces = {"application/text;charset=utf-8"})
        @ResponseBody
        public String pie(@PathVariable("projectid") int projectid){

            LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new QueryWrapper<SysUser>().lambda()
                    .select(SysUser::getUserJob)
                    .eq(SysUser::getProjectId,projectid)
                    .groupBy(SysUser::getUserJob);
            Integer st1 = sysUserMapper.selectCount(lambdaQueryWrapper.eq(SysUser::getUserJob,"Frontend Developer" ));

            int a1;

            try {
                a1 = st1.intValue();
            }
            catch (NullPointerException e) {
                a1 = 0;
            }
            LambdaQueryWrapper<SysUser> lambdaQueryWrapper1 = new QueryWrapper<SysUser>().lambda()
                    .select(SysUser::getUserJob)
                    .eq(SysUser::getProjectId,projectid)
                    .groupBy(SysUser::getUserJob);
            Integer st2 = sysUserMapper.selectCount(lambdaQueryWrapper1.eq(SysUser::getUserJob,"Tester"));
            int a2;

            try {
                a2 = st2.intValue();
            }
            catch (NullPointerException e) {
                a2 = 0;
            }
            LambdaQueryWrapper<SysUser> lambdaQueryWrapper2 = new QueryWrapper<SysUser>().lambda()
                    .select(SysUser::getUserJob)
                    .eq(SysUser::getProjectId,projectid)
                    .groupBy(SysUser::getUserJob);
            Integer st3= sysUserMapper.selectCount(lambdaQueryWrapper2.eq(SysUser::getUserJob,"BackEnd Developer" ));
            int a3;

            try {
                a3 = st3.intValue();
            }
            catch (NullPointerException e) {
                a3 = 0;
            }
            LambdaQueryWrapper<SysUser> lambdaQueryWrapper3 = new QueryWrapper<SysUser>().lambda()
                    .select(SysUser::getUserJob)
                    .eq(SysUser::getProjectId,projectid)
                    .groupBy(SysUser::getUserJob);
            Integer st4 = sysUserMapper.selectCount(lambdaQueryWrapper3.eq(SysUser::getUserJob,"Business Analysis" ));
            int a4;

            try {
                a4 = st4.intValue();
            }
            catch (NullPointerException e) {
                a4 = 0;
            }

            int []datas={a1,a2,a3,a4};
            //需要的数据
            String title = "部门岗位分布";
            String[] searchs = {"Frontend Developer","Tester","BackEnd Developer","Business Analysis"};


            //创建option对象
            Option option = new GsonOption();


            //设置标题  二级标题  标题位置
            option.title().text(title).subtext("岗位详情").x("center");



            //设置显示工具格式
            option.tooltip().show(true).formatter("{a}</br>{b}：{c}/个");

            //设置图例  图例位置  图例对齐方式 竖列对齐
            option.legend().data(searchs).x("left").orient(Orient.vertical);

            //填充数据
            Pie pie = new Pie();//创建饼图对象

            //设置饼图的标题 半径、位置
            pie.name(title).radius("55%").center("50%","50%");

            //填充数据
            for(int i = 0; i < searchs.length; i++){
                Map<String,Object> map = new HashMap<>();
                map.put("value",datas[i]);//填充饼图数据
                map.put("name",searchs[i]);//填充饼图数据对应的搜索引擎
                pie.data(map);
            }
            option.series(pie); //设置数据

            return option.toString();
    }
}